# -*- coding:utf-8 -*-

import os
from time import sleep
from selenium.webdriver.support.ui import Select
from extScript.UploadPic import UploadPic


class CDSY:
    def __init__(self):
        pass

    def cdsy(self, driver, dist=None):
        Select(driver.find_element_by_id("immovablesClass")).select_by_visible_text(u"草地使用权")
        Select(driver.find_element_by_id("meadow-type")).select_by_visible_text(dist["不动产类型2"])

        driver.find_element_by_id("immovablesDetail").clear()
        driver.find_element_by_id("immovablesDetail").send_keys(dist["不动产具体描述"])
        Select(driver.find_element_by_id("immovablesGetWay")).select_by_visible_text(u"赠与")
        driver.find_element_by_id("immovablesLoc").clear()
        driver.find_element_by_id("immovablesLoc").send_keys(dist["不动产坐落"])

        if dist["证件类型"] == "不动产证号":
            driver.find_element_by_id("immovablesNo").clear()
            driver.find_element_by_id("immovablesNo").send_keys(dist["不动产证号"])
        else:
            driver.find_element_by_id("meadowNo").clear()
            driver.find_element_by_id("meadowNo").send_keys(dist["权证编号"])

        if dist["不动产类型2"] == "本集体经济组织以外单位或个人承包经营":
            driver.find_element_by_name("grassRight.collectiveCommentsGraph").click()

            # 本集体经济组织书面意见电子件或照片
            UploadPic(driver, "/html/body/content/div[1]/form/div/div[12]/div[6]/div[3]/div[1]/div[1]/div[2]/div",
                      dist["本集体经济组织书面意见电子件或照片"]).upfile(
                "/html/body/content/div[1]/form/div/div[12]/div[6]/div[3]/div[1]/div[1]/div[2]/a")

            # 乡镇人民政府同意承包的书面意见电子件或照片
            UploadPic(driver, "/html/body/content/div[1]/form/div/div[12]/div[8]/div[3]/div[1]/div[1]/div[2]/div",
                      dist["乡镇人民政府同意承包的书面意见电子件或照片"]).upfile(
                "/html/body/content/div[1]/form/div/div[12]/div[8]/div[3]/div[1]/div[1]/div[2]/a")

            driver.execute_script("arguments[0].scrollIntoView();", driver.find_element_by_id("next-step2"))

            driver.find_element_by_name("interestedPartyView").click()
        else:  # dist["不动产类型2"]==本集体经济组织成员承包经营
            driver.find_element_by_name("grassRight.contractAuthorise").click()

            UploadPic(driver, "/html/body/content/div[1]/form/div/div[12]/div[3]/div[3]/div[1]/div[1]/div[2]/div",
                      dist["《草原承包经营合同》电子件或照片"]).upfile(
                "/html/body/content/div[1]/form/div/div[12]/div[3]/div[3]/div[1]/div[1]/div[2]/a")

            driver.execute_script("arguments[0].scrollIntoView();", driver.find_element_by_id("next-step2"))

            driver.find_element_by_xpath('/html/body/content/div[1]/form/div/div[16]/div[3]/input').click()

        # 利害关系人意见证明材料
        UploadPic(driver, "/html/body/content/div[1]/form/div/div[18]/div[3]/div[1]/div[1]/div[2]/div",
                  dist["利害关系人意见证明材料"]).upfile(
            "/html/body/content/div[1]/form/div/div[18]/div[3]/div[1]/div[1]/div[2]/a")

        driver.execute_script("arguments[0].scrollIntoView();", driver.find_element_by_id("next-step2"))

        driver.find_element_by_id("textarea2").clear()
        driver.find_element_by_id("textarea2").send_keys(u"备注")
        driver.find_element_by_id("next-step2").click()
